package com.account.web;

import com.account.RandomMsg;
import com.account.annotation.loginAnnotation;
import com.account.entity.Account;
import com.account.service.AccountService;
import com.account.service.AccountService2;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/account")
public class AccountController {

    @Autowired
    AccountService accountService;

    @Autowired
    AccountService2 accountService2;

    //测试transactional...@Transactional

    @RequestMapping(value = "/transactional" ,method = RequestMethod.GET)
    public String  testTransactional(){
        String result;
        try{
            accountService2.testTransacation();
            result= "没有异常";
        }catch (ArithmeticException e){
            e.printStackTrace();
            result="运算异常";
        }
        return result;
    }

    @loginAnnotation
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public List<Account> getAccounts() {
        return accountService.findAccountList();
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public Account getAccountById(@PathVariable("id") int id, HttpServletRequest httpServletRequest,HttpServletResponse response) {
        String sessionId=httpServletRequest.getSession().getId();
        System.out.println("################################"+sessionId);
        response.addHeader("Connection","close");
        return accountService.findAccount(id);
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
    public String updateAccount(@PathVariable("id") int id, @RequestParam(value = "name", required = true) String name,
                                @RequestParam(value = "money", required = true) double money) {
        int t= accountService.update(name,money,id);
        if(t==1) {
            return "success";
        }else {
            return "fail";
        }

    }

    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public String delete(@PathVariable(value = "id")int id) {
        int t= accountService.delete(id);
        if(t==1) {
            return "success";
        }else {
            return "fail";
        }

    }

    @RequestMapping(value = "", method = RequestMethod.POST)
    public String postAccount(@RequestParam(value = "name") String name,
                              @RequestParam(value = "money") double money,
                              @RequestParam(value = "msg") String msg) {

        int t= accountService.add(UUID.randomUUID().toString(),name,money,msg);
        if(t==1) {
            return "success";
        }else {
            return "fail";
        }
    }

    @RequestMapping(value = "/batch", method = RequestMethod.GET)
    public String insterBatchData(@RequestParam(value = "count") int count) {
        if (count < 0) {
            return "count must > 0";
        }
        try {
            for (int i = 0; i < count; i++) {
                String name = RandomMsg.getCharRandom(5);
                String money = RandomMsg.getNUmRandom(2);
                int countNUm = Integer.valueOf(RandomMsg.getNUmRandom(1));
                String msg = "";
                for (int J = 1; J <= countNUm; J++) {
                    String m = RandomMsg.getNUmRandom(J);
                    if (J == countNUm) {
                        msg += m;
                    } else {
                        msg += m + ",";
                    }
                }
                accountService.add(UUID.randomUUID().toString(),name, Integer.valueOf(money), msg);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return e.getMessage();
        }
        return count + " data inster success";
    }
}